import 'package:city_pickers/city_pickers.dart';
import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:kzcity_picker/kzcity_picker.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/config/config.dart';
import 'package:yp/helper.dart';
import 'package:yp/pages/address_create/widgets/text_input.dart';
import 'package:yp/widgets/button_widget.dart';
import 'package:yp/pages/address_selector/cy_city_picker.dart';

import 'logic.dart';
import 'state.dart';

class AddressCreatePage extends StatelessWidget {
  final AddressCreateLogic logic = Get.put(AddressCreateLogic());
  final AddressCreateState state = Get.find<AddressCreateLogic>().state;

  @override
  Widget build(BuildContext context) {
    state.userNumid = Get.arguments != null ? Get.arguments["userNum"] : 0;
    return Scaffold(
        resizeToAvoidBottomInset: false,
        appBar: AppBar(
          backgroundColor: Config.mainColor,
          brightness: Brightness.light,
          iconTheme: IconThemeData(color: Colors.white),
          title: Text(
            "新增地址",
            style: TextStyle(color: Colors.white, fontSize: 32.sp),
          ),
          centerTitle: true,
          elevation: 0,
        ),
        body: GetBuilder<AddressCreateLogic>(builder: (logic) {
          return Column(
            children: [
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    // 收货人
                    TextInputWidget(
                      title: "收货人",
                      hintText: "请填写收货人姓名",
                      controller: state.controller1,
                    ),
                    // 手机号
                    TextInputWidget(
                      title: "手机号",
                      hintText: "请填写11位手机号码",
                      controller: state.controller2,
                    ),
                    // 所在地区
                    TextInputWidget(
                      title: "所在地区",
                      suffixWidget: GestureDetector(
                        onTap: () {
                          _clickEventFunc(context);
                        },
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: [
                            Expanded(
                              child: state.provinceName == ''
                                  ? Text(
                                      "请选择地址",
                                      textAlign: TextAlign.right,
                                      style: TextStyle(
                                        fontSize: 28.sp,
                                        color: Color(0xffcccccc),
                                      ),
                                    )
                                  : Column(
                                crossAxisAlignment: CrossAxisAlignment.end,
                                children: [
                                  Text(
                                    state.controller3.text,
                                    style: TextStyle(
                                      fontSize: 28.sp,
                                      color: Color(0xff333333),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            Icon(
                              Icons.arrow_forward_ios_rounded,
                              color: Color(0xffcccccc),
                              size: 14,
                            )
                          ],
                        ),
                      ),
                    ),
                    // 详细地址
                    TextInputWidget(
                      title: "详细地址",
                      hintText: "请输入详细地址",
                      controller: state.controller4,
                    ),
                    // 标签
                    TextInputWidget(
                      title: "标签",
                      suffixWidget: GetBuilder<AddressCreateLogic>(
                        builder: (l) {
                          return Wrap(
                            children: [
                              GestureDetector(
                                onTap: () {
                                  state.sex = 1;
                                  l.update();
                                },
                                child: Container(
                                  padding: EdgeInsets.symmetric(
                                    horizontal: 30.w,
                                    vertical: 2.w,
                                  ),
                                  margin:
                                      EdgeInsets.only(left: 20.w, bottom: 20.w),
                                  decoration: BoxDecoration(
                                    borderRadius: BorderRadius.circular(40),
                                    border: Border.all(
                                      color: Color(0xffcccccc),
                                    ),
                                    color: state.sex == 1
                                        ? Config.mainColor
                                        : Color(0xffF5F5F5),
                                  ),
                                  child: Text(
                                    state.tagList[0],
                                    style: TextStyle(
                                      color: state.sex == 1
                                          ? Colors.white
                                          : Color(0xffcccccc),
                                    ),
                                  ),
                                ),
                              ),
                              GestureDetector(
                                onTap: () {
                                  state.sex = 2;
                                  l.update();
                                },
                                child: Container(
                                  padding: EdgeInsets.symmetric(
                                    horizontal: 30.w,
                                    vertical: 2.w,
                                  ),
                                  margin:
                                      EdgeInsets.only(left: 20.w, bottom: 20.w),
                                  decoration: BoxDecoration(
                                    borderRadius: BorderRadius.circular(40),
                                    border: Border.all(
                                      color: Color(0xffcccccc),
                                    ),
                                    color: state.sex == 2
                                        ? Config.mainColor
                                        : Color(0xffF5F5F5),
                                  ),
                                  child: Text(
                                    state.tagList[1],
                                    style: TextStyle(
                                      color: state.sex == 2
                                          ? Colors.white
                                          : Color(0xffcccccc),
                                    ),
                                  ),
                                ),
                              ),
                              if (state.tagList[2] != "")
                                GestureDetector(
                                  onTap: () {
                                    state.sex = 3;
                                    l.update();
                                  },
                                  child: Container(
                                    padding: EdgeInsets.symmetric(
                                      horizontal: 30.w,
                                      vertical: 2.w,
                                    ),
                                    margin: EdgeInsets.only(
                                        left: 20.w, bottom: 20.w),
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(40),
                                      border: Border.all(
                                        color: Color(0xffcccccc),
                                      ),
                                      color: state.sex == 3
                                          ? Config.mainColor
                                          : Color(0xffF5F5F5),
                                    ),
                                    child: Text(
                                      state.tagList[2],
                                      style: TextStyle(
                                        color: state.sex == 3
                                            ? Colors.white
                                            : Color(0xffcccccc),
                                      ),
                                    ),
                                  ),
                                ),
                              GestureDetector(
                                onTap: () {
                                  showDialog(
                                    context: context,
                                    barrierDismissible: false,
                                    builder: (BuildContext context) {
                                      return AlertDialog(
                                        title: Text('添加地址标签'),
                                        content: TextField(
                                          controller: state.tagController,
                                          textAlignVertical:
                                              TextAlignVertical.center,
                                          onChanged: (value) {},
                                          decoration: InputDecoration(
                                            isDense: true,
                                            border: InputBorder.none,
                                            hintText: '请输入自定义标签',
                                            hintStyle: TextStyle(
                                              fontSize: 28.sp,
                                              color: Color(0xffcccccc),
                                            ),
                                          ),
                                        ),
                                        actions: <Widget>[
                                          FlatButton(
                                            onPressed: () =>
                                                Navigator.of(context).pop(),
                                            child: Text('取消'),
                                          ),
                                          FlatButton(
                                            onPressed: () {
                                              if (state.tagController.text
                                                  .isNotEmpty) {
                                                SpUtil.putString("addressTag",
                                                    state.tagController.text);

                                                state.tagList[2] =
                                                    state.tagController.text;

                                                l.update();
                                              }

                                              Navigator.of(context).pop();
                                            },
                                            textColor: Colors.red,
                                            child: Text('确定'),
                                          ),
                                        ],
                                      );
                                    },
                                  );
                                },
                                child: Container(
                                  padding: EdgeInsets.symmetric(
                                    horizontal: 30.w,
                                    vertical: 2.w,
                                  ),
                                  margin: EdgeInsets.only(left: 20.w),
                                  decoration: BoxDecoration(
                                    borderRadius: BorderRadius.circular(40),
                                    border: Border.all(
                                      color: Color(0xffcccccc),
                                    ),
                                  ),
                                  child: Text(
                                    "自定义",
                                    style: TextStyle(color: Color(0xffcccccc)),
                                  ),
                                ),
                              ),
                            ],
                          );
                        },
                      ),
                    ),
                    TextInputWidget(
                      title: "设为默认",
                      suffixWidget: GetBuilder<AddressCreateLogic>(
                        builder: (logic) {
                          return Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              Switch(
                                value: state.isSelect,
                                inactiveThumbColor: Colors.white,
                                inactiveTrackColor: Colors.grey,
                                activeColor: Colors.white,
                                activeTrackColor: Colors.blue,
                                onChanged: (isSelect) {
                                  state.isSelect = isSelect;
                                  logic.update();
                                },
                              )
                            ],
                          );
                        },
                      ),
                    ),
                  ],
                ),
              ),
              Container(
                color: Colors.white,
                padding: EdgeInsets.symmetric(
                  vertical: 30.w,
                  horizontal: 46.w,
                ),
                child: GestureDetector(
                  onTap: () {
                    if (state.controller1.text.isEmpty) {
                      toast("请输入收货人姓名");
                      return;
                    }
                    if (state.controller2.text.isEmpty) {
                      toast("请输入手机号");
                      return;
                    }
                    if (!RegexUtil.isMobileSimple(state.controller2.text)) {
                      toast("手机号格式错误");
                      return;
                    }
                    if (state.controller3.text.isEmpty) {
                      toast("请选择所在地区");
                      return;
                    }
                    if (state.controller4.text.isEmpty) {
                      toast("请填写详细地址");
                      return;
                    }
                    logic.addAddress();
                  },
                  child: Padding(
                    padding: EdgeInsets.symmetric(
                      horizontal: 20.w,
                    ),
                    child: ButtonWidget(title: "保存并添加"),
                  ),
                ),
              ),
            ],
          );
        }));
  }

  Result resultArr = new Result();

  void _clickEventFunc(context) async {
    // final res = await KzcityPicker.showPicker(
    //   context,
    //   datas: state.addressList,
    // );
    // if (res != null) {
    //   // 刷新
    //   logic.selectCity(res);
    // }

    XYZCityPicker.showPicker(
        province: state.province,
        city: state.city,
        district: state.district,
        street: state.street,
        provinceListBack: state.provinceList,
        cityListBack: state.cityList,
        cityAreaListBack: state.cityAreaList,
        townListBack: state.townList,
        context: context,
        onChange: (int index, Map checkedItem, List backAddressItemList) {
          logic.selectCity(index, checkedItem, backAddressItemList);
        });
  }
}
